﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;

namespace XGameFrame
{
    /// <summary>
    /// Class to handle the camera position
    /// </summary>
    public class MapCamera
    {
        #region Field Region
        public Vector2 location;
        private Vector2 displayOffset;

        private int viewWidth;
        private int viewHeight;
        private int worldWidth;
        private int worldHeight; 
        #endregion

        #region Property Region
        /// <summary>
        /// Get the actual camera location or set it
        /// </summary>
        public Vector2 Location
        {
            get { return location; }
            set
            {
                location = new Vector2(
                    MathHelper.Clamp(value.X, 0f, WorldWidth - ViewWidth),
                    MathHelper.Clamp(value.Y, 0f, WorldHeight - ViewHeight));
            }
        }

        /// <summary>
        /// Get the DisplayOffset or set it
        /// </summary>
        public Vector2 DisplayOffset
        {
            get { return displayOffset; }
            set { displayOffset = value; }
        }

        /// <summary>
        /// Get the width of view or set it
        /// </summary>
        public int ViewWidth
        {
            get { return viewWidth; }
            set { viewWidth = value; }
        }

        /// <summary>
        /// Get the height of view or set it
        /// </summary>
        public int ViewHeight
        {
            get { return viewHeight; }
            set { viewHeight = value; }
        }

        /// <summary>
        /// Get the width of world or set it
        /// </summary>
        public int WorldWidth
        {
            get { return worldWidth; }
            set { worldWidth = value; }
        }

        /// <summary>
        /// Get the height of world or set it
        /// </summary>
        public int WorldHeight
        {
            get { return worldHeight; }
            set { worldHeight = value; }
        } 
        #endregion

        public MapCamera(MapBase pMap)
        {
            ViewWidth = 1024;
            ViewHeight = 768;
            WorldWidth = pMap.MapWidth * (int)pMap.TileHandler.TileStep.X;
            WorldHeight = pMap.MapHeight * (int)pMap.TileHandler.TileStep.Y;
            DisplayOffset = new Vector2(pMap.TileHandler.PosiitionCorrection.X,pMap.TileHandler.PosiitionCorrection.Y);
        }

        #region Method Region
        public Vector2 WorldToScreen(Vector2 worldPosition)
        {
            return worldPosition - Location + DisplayOffset;
        }

        public Vector2 ScreenToWorld(Vector2 screenPosition)
        {
            return screenPosition + Location - DisplayOffset;
        }

        public void Move(Vector2 offset)
        {
            Location += offset;
        } 
        #endregion
    }
}
